//
//  FCWebVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/4/10.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCWebVC.h"
#import <WebKit/WebKit.h>
@interface FCWebVC ()<WKNavigationDelegate,WKUIDelegate>{
    WKWebView *webView;
    UIView     *_viewNav;
    NSString *_copyStr;
    UILabel *_backLabel;
}

@end

@implementation FCWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建wkwebview
    webView = [[WKWebView alloc]init];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(64);
        make.bottom.equalTo(self.view);
    }];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    //拼接url
    NSString *webUrl = [NSString stringWithFormat:@"%@%@",self.urlStr,self.transactionIdStr];
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webUrl]]];
    
    //kvo
    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
//设置导航栏
-(void)setNav{
    
    self.navigationController.navigationBarHidden =YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(84);
            }else{
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(64);
            }
            
            
        }else{
            make.left.right.top.mas_equalTo(0);
            make.height.mas_equalTo(64);
        }
    }];
    _viewNav.backgroundColor =HEXCOLOR(0x171212);
    
    UIButton *backBtn = [UIButton new];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(50);
    }];
    [backBtn setImage:[UIImage imageNamed:@"back-1"] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    //返回
    UILabel *backLabel = [UILabel new];
    _backLabel = backLabel;
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(backBtn.mas_right).mas_offset(20);
        make.centerY.mas_equalTo(backBtn);
        make.right.mas_equalTo(_viewNav.mas_right).mas_offset(-50);
    }];
    //    backLabel.text = Localized(@"百度一下");
    backLabel.textColor = [UIColor whiteColor];
    
}
#pragma mark-UIbutton事件
-(void)backBtn{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
    //设置导航栏
    [self setNav];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    
    
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    
}

// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    
    NSLog(@"%@",navigationResponse.response);
    //允许跳转
    _copyStr = navigationResponse.response.URL.absoluteString;
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    NSLog(@"%@",navigationAction.request.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationActionPolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationActionPolicyCancel);
}

//释放通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [webView removeObserver:self forKeyPath:@"title"];
}

@end
